import { ref } from "vue";
import { defineStore } from 'pinia'
export const useCounterStore = defineStore('cart', () => {
    let carts = ref([])
    function addcart(detail, goodsid) {
        let username = localStorage.getItem('username')
        let usercart = carts.value.filter(item => item.username == username)
        let pdcart = usercart.find(item => item.goods.goods_id == goodsid)
        if (pdcart) {
            pdcart.number++
        } else {
            carts.value.push({
                username: username,
                id: Math.floor(Math.random() * 100000),
                checked: false,
                number: 1,
                goods: detail,
            })

        }
    }
    return { carts, addcart }
},
    {
        persist: true,
    },
)